import 'dart:io';

import 'package:fate_blind_box/chat_view/ahu_text.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/ui/account/scan/scan_main_page.dart';
import 'package:fate_blind_box/ui/user/profile.dart';
import 'package:fate_blind_box/utils/default/common_config.dart';
import 'package:fate_blind_box/utils/page.dart';
import 'package:fate_blind_box/utils/provider.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:tcard/tcard.dart';

import 'logic.dart';

class MeetBlindBoxPage extends GetView<MeetBlindBoxLogic> {
  const MeetBlindBoxPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.transparent,
      body: Obx(() {
        return Container(
          width: double.infinity,
          height: double.infinity,
          decoration: const BoxDecoration(
              image: DecorationImage(
                  image: AssetImage('images/scan/ccym_center_bg.png'),
                  fit: BoxFit.cover)),
          child: SingleChildScrollView(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                SizedBox(
                  height: ScreenUtil().statusBarHeight,
                ),
                SizedBox(
                  height: 47.w,
                ),
                CommonConfig.isShowPay() == false
                    ? Text(
                        '都肆商圈',
                        style: TextStyle(
                            fontSize: 18,
                            color: ThemeColor.black1414Color,
                            fontWeight: FontWeight.w600),
                      )
                    : Image.asset(
                        'images/scan/yjmh_bg_font.png',
                        width: 140,
                      ),
                Offstage(
                  offstage: CommonConfig.isShowPay() == false,
                  child: Text(
                    '拆拆我是谁',
                    style: TextStyle(
                        fontSize: 14, color: ThemeColor.hint777777Color),
                  ),
                ),
                const SizedBox(
                  height: 32,
                ),
                initSwiper(),
                const SizedBox(
                  height: 18,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    scanView(),
                    addChance(),
                    unfoldView(),
                  ],
                ),
                SizedBox(height: 44.w),
              ],
            ),
          ),
        );
      }),
    );
  }

  Widget initSwiper() {
    if (controller.state.blindList.isEmpty ||
        (controller.state.isRight && controller.state.listData.isEmpty)) {
      return controller.checkType(0);
    } else {
      return Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Image.asset(
            'images/box_left.png',
            width: 28,
            height: 342,
          ),
          Stack(
            children: [
              TCard(
                  controller: controller.state.tCardController,
                  onEnd: () {
                    print('---2-----------');
                    if (controller.state.isRight) {
                      controller.state.chenkid = 0;
                      controller.state.tCardController.reset();
                    }
                  },
                  onBack: (index, info) {
                    print('---1-----------');
                  },
                  onForward: (index, info) {
                    print('---3-----------');
                    if (info.direction == SwipDirection.Left) {
                      if (controller.state.isRight == true) {
                        controller.state.blindList.clear();
                        controller.getData();
                      }
                      controller.state.isRight = false;
                      controller.state.chenkid = index;
                      controller.state.blindList.removeAt(0);
                      controller.state.isOpenSign.removeAt(0);
                      if (controller.state.blindList.isEmpty) {
                        controller.getData();
                      }
                      controller.state.tCardController.state?.setState(() {});
                    } else if (info.direction == SwipDirection.Right) {
                      controller.state.chenkid = index;
                      if (controller.state.isSeedRight == false ||
                          controller.state.isRight == false) {
                        controller.state.chenkid = 0;
                        controller.getMeLookList(1);
                      }
                      controller.state.isRight = true;
                      controller.state.tCardController.state?.setState(() {});
                    }
                  },
                  size: Size(296.w, 335.w),
                  cards: !controller.state.isRight
                      ? controller.state.blindList
                          .map((e) => Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10.0),
                                  boxShadow: const [
                                    BoxShadow(
                                      offset: Offset(0, 17),
                                      blurRadius: 23.0,
                                      spreadRadius: -13.0,
                                      color: Colors.black54,
                                    )
                                  ],
                                ),
                                child: GestureDetector(
                                  onTap: () {
                                    if (controller.state.isOpenSign[controller
                                            .state.blindList
                                            .indexOf(e)]["isOpenSign"] ==
                                        1) {
                                      Navigate.toPage(UserProfileUi(
                                          memberId: e.id.toString()));
                                    } else {
                                      controller.blindOpen(e.id.toString());
                                    }
                                  },
                                  child: ClipRRect(
                                      borderRadius: BorderRadius.circular(10.0),
                                      child: Stack(
                                        children: [
                                          Image.network(
                                            Provider.fixAvatar(e.profilePhoto),
                                            fit: BoxFit.cover,
                                            width: 296.w,
                                            height: 335.w,
                                          ),
                                          // checktype(_blindList[chenkid]["openSign"]),
                                          controller.checkType(controller
                                                  .state.isOpenSign[
                                              controller.state.blindList
                                                  .indexOf(e)]["isOpenSign"])
                                        ],
                                      )),
                                ),
                              ))
                          .toList()
                      : controller.state.listData
                          .map((e) => Container(
                                decoration: BoxDecoration(
                                  color: Colors.white,
                                  borderRadius: BorderRadius.circular(10.0),
                                  boxShadow: const [
                                    BoxShadow(
                                      offset: Offset(0, 17),
                                      blurRadius: 23.0,
                                      spreadRadius: -13.0,
                                      color: Colors.black54,
                                    )
                                  ],
                                ),
                                child: GestureDetector(
                                  onTap: () {
                                    if (controller.state.seedData.isEmpty) {
                                      controller.state.isOnlyOneRight = true;
                                      controller.blindOpen(e['id']);
                                    } else {
                                      Navigate.toPage(
                                          UserProfileUi(memberId: e['id']));
                                    }
                                  },
                                  child: ClipRRect(
                                      borderRadius: BorderRadius.circular(10.0),
                                      child: Stack(
                                        children: [
                                          Image.network(
                                            Provider.fixAvatar(
                                                e['profilePhoto']),
                                            fit: BoxFit.cover,
                                            width: 296.w,
                                            height: 335.w,
                                          ),
                                          controller.state.isOpenSign[0]
                                                          ['isOpenSign'] ==
                                                      1 ||
                                                  controller
                                                      .state.seedData.isNotEmpty
                                              ? controller.checkType(1)
                                              : controller.checkType(0)
                                        ],
                                      )),
                                ),
                              ))
                          .toList()),
              !controller.state.isRight
                  ? Positioned(
                      bottom: 40,
                      right: 35,
                      child: Visibility(
                        visible: CommonConfig.isShowPay(),
                        child: GestureDetector(
                          onTap: () {
                            if (controller.state.totalCount < 1) {
                              controller.jumpPayView();
                            }
                          },
                          child: Container(
                            padding: const EdgeInsets.symmetric(
                                horizontal: 8, vertical: 4),
                            decoration: BoxDecoration(
                                color: const Color(0xFFEBFCFF),
                                border: Border.all(
                                    color: const Color.fromRGBO(
                                      95,
                                      126,
                                      240,
                                      0.29,
                                    ),
                                    width: 1),
                                borderRadius: BorderRadius.circular(23)),
                            child: Text(
                              '剩余${controller.state.totalCount}次机会',
                              style: const TextStyle(
                                  color: Color(
                                    0xFF5C7DF5,
                                  ),
                                  fontSize: 12),
                            ),
                          ),
                        ),
                      ),
                    )
                  : const SizedBox()
            ],
          ),
          Image.asset(
            'images/box_right.png',
            width: 28,
            height: 342,
          ),
        ],
      );
    }
  }

  /// 扫一扫
  Widget scanView() {
    return InkWell(
      onTap: () {
        Get.to(ScanMainPage(
          refreshBlock: (codeStr) {
            controller.getLatestMatchData(codeStr);
          },
        ));
      },
      child: Container(
        width: 81.w,
        height: 46.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(23.w),
            boxShadow: const [
              BoxShadow(
                  color: Color.fromRGBO(88, 171, 247, 0.15),
                  blurStyle: BlurStyle.solid,
                  blurRadius: 4,
                  offset: Offset(0, 4))
            ],
            gradient: const LinearGradient(
              colors: [
                Color(0xFF99BBFF),
                Color(0xFF58ABF7),
              ],
            )),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            const AhuText(
              text: '扫',
              fontSize: 18,
              fontWeight: FontWeight.w400,
              color: Colors.white,
            ),
            SizedBox(width: 6.w),
            Image.asset(
              'images/scan/main_scan_white.png',
              width: 18.w,
              height: 18.w,
              fit: BoxFit.contain,
            ),
            SizedBox(width: 6.w),
            const AhuText(
              text: '扫',
              fontSize: 18,
              fontWeight: FontWeight.w400,
              color: Colors.white,
            ),
          ],
        ),
      ),
    );
  }

  /// 增加机会
  Widget addChance() {
    if (Platform.isAndroid ? true : CommonConfig.isShowPay()) {
      return InkWell(
        onTap: () {
          controller.jumpPayView();
        },
        child: Container(
          width: 124.w,
          height: 46.w,
          alignment: Alignment.center,
          margin: EdgeInsets.symmetric(horizontal: 14.w),
          decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(23.w),
            boxShadow: [
              BoxShadow(
                  color: '#4C7F69D9'.stringToColor,
                  blurStyle: BlurStyle.solid,
                  blurRadius: 4,
                  offset: const Offset(0, 4))
            ],
            gradient: const LinearGradient(
              colors: [
                Color(0XFFD175FE),
                Color(0XFF747CF8),
              ],
            ),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Image.asset(
                'images/scan/main_scan_add.png',
                width: 18.w,
              ),
              SizedBox(width: 3.w),
              Text(
                '增加机会',
                style: TextStyle(
                    color: Colors.white,
                    fontSize: 17.sp,
                    fontWeight: FontWeight.w600),
              ),
            ],
          ),
        ),
      );
    }
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 14.w),
      width: 124.w,
    );
  }

  /// 点点看
  Widget unfoldView() {
    return InkWell(
      onTap: () {
        if (!(controller.state.isRight)) {
          if (controller.state.isOpenSign.first['isOpenSign'] != 1) {
            if (controller.state.blindList.first.id.toString().isEmpty) return;
            controller
                .blindOpen(controller.state.blindList.first.id.toString());
          } else {
            Navigator.push(Get.context!, CupertinoPageRoute(builder: (context) {
              return UserProfileUi(
                  memberId: '${controller.state.blindList.first.id}');
            }));
            // Navigate.toPage(UserProfileUi(
            //     memberId: controller.state.blindList.first.id.toString()));
          }
        } else {
          if (controller.state.seedData.isEmpty) {
            controller.state.isOnlyOneRight = true;
            controller.blindOpen(controller.state.listData[0]['id']);
          } else {
            if (controller.state.isOnlyOneRight ||
                controller.state.seedData.length == 1) {
              Navigate.toPage(
                  UserProfileUi(memberId: controller.state.listData[0]['id']));
            } else {
              Navigate.toPage(UserProfileUi(
                  memberId: controller.state.listData[controller.state.chenkid]
                      ['id']));
            }
          }
        }
      },
      child: Container(
        width: 81.w,
        height: 46.w,
        alignment: Alignment.center,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(23.w),
          boxShadow: const [
            BoxShadow(
                color: Color.fromRGBO(247, 88, 88, 0.15),
                blurStyle: BlurStyle.solid,
                blurRadius: 4,
                offset: Offset(0, 4))
          ],
          gradient: const LinearGradient(
            colors: [
              Color(0XFFFF9999),
              Color(0XFFF75858),
            ],
          ),
        ),
        child: Text(
          CommonConfig.isShowPay() == false ? '点点看' : '拆拆看',
          style: TextStyle(
              color: Colors.white,
              fontSize: 18.sp,
              fontWeight: FontWeight.w600),
        ),
      ),
    );
  }
}
